#!/bin/sh
#
# builddeb 1.3
# Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
#
# Simple script to generate a deb package for a Linux kernel. All the
# complexity of what to do with a kernel after it is installed or removed
# is left to other scripts and packages: they can install scripts in the
# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
# specified in KDEB_HOOKDIR) that will be called on package install and
# removal.
#
# armbian/bulddeb 0.1
# Copyright 2021 Leonid Gasheev (The-going <48602507+The-going@users.noreply.github.com>)
#
# In order to make a choice, it is enough for us to check the value
# of the variable in the kernel configuration file and Debian's own variables.
#
# Uncomment the next line to see all the current values.
# echo "$(dpkg-architecture -l)" >&2

set -e

is_enabled() {
	grep -q "^$1=y" include/config/auto.conf
}

if_enabled_echo() {
	if is_enabled "$1"; then
		echo -n "$2"
	elif [ $# -ge 3 ]; then
		echo -n "$3"
	fi
}

is_native() {
	dpkg-architecture -qDEB_BUILD_ARCH | grep -q "$(dpkg-architecture -qDEB_HOST_ARCH)"
}

is_build_on_amd64() {
	dpkg-architecture -qDEB_BUILD_ARCH | grep -q amd64
}

create_package() {
	local pname="$1" pdir="$2"
	local dpkg_deb_opts

	mkdir -m 755 -p "$pdir/DEBIAN"
	mkdir -p "$pdir/usr/share/doc/$pname"
	cp debian/copyright "$pdir/usr/share/doc/$pname/"
	cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
	gzip -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
	sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
		| xargs -r0 md5sum > DEBIAN/md5sums"

	# Fix ownership and permissions
	if [ "$DEB_RULES_REQUIRES_ROOT" = "no" ]; then
		dpkg_deb_opts="--root-owner-group"
	else
		chown -R root:root "$pdir"
	fi
	chmod -R go-w "$pdir"
	# in case we are in a restrictive umask environment like 0077
	chmod -R a+rX "$pdir"
	# in case we build in a setuid/setgid directory
	chmod -R ug-s "$pdir"

	# Create preinstall and post install script to remove dtb
	if [ "$3" = "dtb" ]; then

		cat >> $pdir/DEBIAN/preinst <<- EOT
			rm -rf /boot/dtb
			rm -rf /boot/dtb-$version
			exit 0
		EOT

		cat >> $pdir/DEBIAN/postinst <<- EOT
			cd /boot
			ln -sfT dtb-$version dtb 2> /dev/null || mv dtb-$version dtb
			exit 0
		EOT

		chmod 775 $pdir/DEBIAN/preinst
		chmod 775 $pdir/DEBIAN/postinst
	fi

	# Create postinst prerm script for headers
	if [ "$3" = "headers" ]; then

		# Set the time for all files to the current time.
		# And build them for the current architecture.
		cat >> $pdir/DEBIAN/postinst <<- EOT
			cd /usr/src/linux-headers-$version
			echo "Compiling headers - please wait ..."
			NCPU=\$(grep -c 'processor' /proc/cpuinfo)
			find -type f -exec touch {} +
			yes "" | make ARCH=$ARCH oldconfig >/dev/null
			make -j\$NCPU ARCH=$ARCH -s scripts >/dev/null
			make -j\$NCPU ARCH=$ARCH -s M=scripts/mod/ >/dev/null
			exit 0
		EOT

		# After the configuration and compilation processes, new files
		# appear that the package manager does not know anything about.
		# Just clear all the files in the target directory.
		cat >> $pdir/DEBIAN/prerm <<- EOT
			rm -rf /usr/src/linux-headers-$version
			exit 0
		EOT

		chmod 775 $pdir/DEBIAN/postinst
		chmod 775 $pdir/DEBIAN/prerm
	fi

	# Create the package
	dpkg-gencontrol -p$pname -P"$pdir"
	dpkg-deb ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" ..
}

deploy_kernel_headers() {
	pdir=$1

	rm -rf $pdir

	destdir=$pdir/usr/src/linux-headers-$version
	mkdir -p $destdir

	if linux-version compare "$KERNELVERSION" ge "5.19" ; then
		configobj=CONFIG_OBJTOOL
	else
		configobj=CONFIG_STACK_VALIDATION
	fi

	(
		cd $srctree
		find . -name Makefile\* -o -name Kconfig\* -o -name \*.pl
		find arch/*/include include scripts -type f -o -type l
		find security/*/include -type f
		find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform
		find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
	) > debian/hdrsrcfiles

	{
		# This affects arch/x86
		if is_enabled $configobj; then
			#	echo tools/objtool/objtool
			find tools/objtool -type f -executable
		fi

		find arch/$SRCARCH/include Module.symvers include scripts -type f

		if is_enabled CONFIG_GCC_PLUGINS; then
			find scripts/gcc-plugins -name \*.so -o -name gcc-common.h
		fi
	} > debian/hdrobjfiles

	if is_native; then
		echo "info: Build native: Skip headers-debian-byteshift.patch" >&2
	elif is_build_on_amd64; then
		(
			cd $destdir
			patch -p1 < /tmp/headers-debian-byteshift.patch
		)
	fi

	tar -c -f - -C $srctree -T debian/hdrsrcfiles | tar -xf - -C $destdir
	tar -c -f - -T debian/hdrobjfiles | tar -xf - -C $destdir
	rm -f debian/hdrsrcfiles debian/hdrobjfiles

	# copy .config manually to be where it's expected to be
	cp $KCONFIG_CONFIG $destdir/.config

	mkdir -p $pdir/lib/modules/$version/
	ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
}

deploy_libc_headers() {
	pdir=$1

	rm -rf $pdir

	$MAKE -f $srctree/Makefile headers
	$MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr

	# move asm headers to /usr/include/<libc-machine>/asm to match the structure
	# used by Debian-based distros (to support multi-arch)
	host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH)
	mkdir $pdir/usr/include/$host_arch
	mv $pdir/usr/include/asm $pdir/usr/include/$host_arch/
}

version=$KERNELRELEASE
tmpdir="debian/tmp"
kernel_headers_dir="debian/hdrtmp"
libc_headers_dir="debian/headertmp"
dbg_dir="debian/dbgtmp"
dtb_dir="debian/dtbtmp"
packagename=linux-image-"$BRANCH$LOCALVERSION"
kernel_headers_packagename=linux-headers-"$BRANCH$LOCALVERSION"
dtb_packagename=linux-dtb-"$BRANCH$LOCALVERSION"
libc_headers_packagename=linux-libc-dev
dbg_packagename=$packagename-dbg

if [ "$ARCH" = "um" ]; then
	packagename=user-mode-linux-$version
fi

# Not all arches have the same installed path in debian
# XXX: have each arch Makefile export a variable of the canonical image install
# path instead
case $ARCH in
	++aarch64|arm64)
		image_name=Image
		installed_image_path="boot/vmlinuz-$version"
		;;
	arm*)
		image_name=zImage
		installed_image_path="boot/vmlinuz-$version"
		;;
	um)
		installed_image_path="usr/bin/linux-$version"
		;;
	parisc|mips|powerpc)
		installed_image_path="boot/vmlinux-$version"
		;;
	*)
		image_name=vmlinuz
		installed_image_path="boot/vmlinuz-$version"
		;;
esac

BUILD_DEBUG=$(if_enabled_echo CONFIG_DEBUG_INFO Yes)

# Setup the directory structure
rm -rf "$tmpdir" "$dbg_dir" "$dtb_dir" debian/files
mkdir -m 755 -p "$tmpdir/DEBIAN"
mkdir -p "$tmpdir/lib" "$tmpdir/boot"
mkdir -m 755 -p "$dtb_dir/DEBIAN"
mkdir -p "$dtb_dir/boot/dtb-$version" "$dtb_dir/usr/share/doc/$dtb_packagename"
mkdir -m 755 -p "$kernel_headers_dir/lib/modules/$version/"
mkdir -m 755 -p "$libc_headers_dir/DEBIAN"

# Install the kernel
if [ "$ARCH" = "um" ]; then
	mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename"
	$MAKE linux
	cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map"
	cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config"
	gzip "$tmpdir/usr/share/doc/$packagename/config"
else
	cp System.map "$tmpdir/boot/System.map-$version"
	cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version"
fi
cp "$($MAKE -s -f $srctree/Makefile image_name)" "$tmpdir/$installed_image_path"

if is_enabled CONFIG_OF_EARLY_FLATTREE; then
	# Only some architectures with OF support have this target
	if [ -d "${srctree}/arch/$SRCARCH/boot/dts" ]; then
		$MAKE -f $srctree/Makefile INSTALL_DTBS_PATH="$tmpdir/usr/lib/linux-image-$version" dtbs_install
	fi
fi

if grep -q '^CONFIG_OF=y' $KCONFIG_CONFIG; then
	#mkdir -p "$tmpdir/boot/dtb"
	INSTALL_DTBS_PATH="$dtb_dir/boot/dtb-$version" $MAKE KBUILD_SRC= dtbs_install
fi

if is_enabled CONFIG_MODULES; then
	INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_install
	rm -f "$tmpdir/lib/modules/$version/build"
	rm -f "$tmpdir/lib/modules/$version/source"

	if [ "$ARCH" = "um" ]; then
		mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/"
		rmdir "$tmpdir/lib/modules/$version"
	fi

	if [ -n "$BUILD_DEBUG" ]; then
		for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do
			module=lib/modules/$module
			mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module)
			# only keep debug symbols in the debug file
			$OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module
			# strip original module from debug symbols
			$OBJCOPY --strip-debug $tmpdir/$module
			# then add a link to those
			$OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module
		done

		# resign stripped modules
		if is_enabled CONFIG_MODULE_SIG_ALL; then
			INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_sign
		fi
	fi
fi

# Install the maintainer scripts
# Note: hook scripts under /etc/kernel are also executed by official Debian
# kernel packages, as well as kernel packages built using make-kpkg.
# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
# so do we; recent versions of dracut and initramfs-tools will obey this.
debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
for script in postinst postrm preinst prerm; do
	mkdir -p "$tmpdir$debhookdir/$script.d"
	cat <<- EOF > "$tmpdir/DEBIAN/$script"
		#!/bin/bash

		set -e

		# Pass maintainer script parameters to hook scripts
		export DEB_MAINT_PARAMS="\$*"

		# Tell initramfs builder whether it's wanted
		export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)

		test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d
		exit 0
	EOF
	chmod 755 "$tmpdir/DEBIAN/$script"
done

##
## Create sym link to kernel image
##
sed -e "s/exit 0//g" -i $tmpdir/DEBIAN/postinst
cat >> $tmpdir/DEBIAN/postinst <<- EOT
	ln -sf $(basename $installed_image_path) /boot/$image_name 2> /dev/null || cp /$installed_image_path /boot/$image_name
	touch /boot/.next
	exit 0
EOT

##
## FAT install workaround
##
sed -e "s/exit 0//g" -i $tmpdir/DEBIAN/preinst
cat >> $tmpdir/DEBIAN/preinst <<- EOT
	# exit if we are running chroot
	if [ "\$(stat -c %d:%i /)" != "\$(stat -c %d:%i /proc/1/root/.)" ]; then exit 0; fi

	check_boot_dev (){
		boot_device=\$(mountpoint -d /boot)

		for file in /dev/* ; do
			CURRENT_DEVICE=\$(printf "%d:%d" \$(stat --printf="0x%t 0x%T" \$file))
			if [[ "\$CURRENT_DEVICE" = "\$boot_device" ]]; then
				boot_partition=\$file
				break
			fi
		done

		bootfstype=\$(blkid -s TYPE -o value \$boot_partition)
		if [ "\$bootfstype" = "vfat" ]; then
			rm -f /boot/System.map* /boot/config* /boot/vmlinuz* /boot/$image_name /boot/uImage
		fi
	}
	mountpoint -q /boot && check_boot_dev
	exit 0
EOT

create_package "$packagename" "$tmpdir"

if [ "$ARCH" != "um" ]; then

	if [ "$(cat debian/arch)" != "amd64" ]; then # No DTB for amd64 target
		create_package "$dtb_packagename" "$dtb_dir" "dtb"
	fi

	deploy_libc_headers $libc_headers_dir
	create_package $libc_headers_packagename $libc_headers_dir

	if is_enabled CONFIG_MODULES; then
		if is_native; then
			# echo "Skip scripts folder cleaning" >&2
			# echo "Skip creating postinst prerm scripts for headers" >&2
			deploy_kernel_headers $kernel_headers_dir
			create_package $kernel_headers_packagename $kernel_headers_dir
		else
			# Clean up the executables that are left over from
			# cross-compilation for a different host architecture.
			(
				cd $srctree
				make M=scripts clean
			)
			deploy_kernel_headers $kernel_headers_dir
			create_package $kernel_headers_packagename $kernel_headers_dir "headers"
		fi
	fi

fi

if [ -n "$BUILD_DEBUG" ]; then
	# Build debug package
	# Different tools want the image in different locations
	# perf
	mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/
	cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/
	# systemtap
	mkdir -p $dbg_dir/usr/lib/debug/boot/
	ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version
	# kdump-tools
	ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version
	create_package "$dbg_packagename" "$dbg_dir"
fi

exit 0
